<template>
  <div style="padding: 10px; display: flex; flex-direction: column; height: 100%;">
    <el-form ref="cameraAiFormRef" :model="form" :rules="rules" label-width="80px"  style="height: calc(100% - 100px); display: flex; flex-direction: column;">
        <el-form-item label="ai算法名称" prop="cameraAiName" label-width="100px">
          <el-input v-model="form.cameraAiName" placeholder="请输入ai算法名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark" label-width="100px">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div style="height: 70px; display: flex; justify-content: center; align-items: end;">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="emit('close')">取 消</el-button>
      </div>
  </div>
</template>
<script lang="ts" setup>
import { cameraAiQuery, cameraAiForm } from '@/api/security/camera/camera-ai/types';
import { getcameraAi, updatecameraAi } from '@/api/security/camera/camera-ai';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;


const buttonLoading = ref(false);
const cameraAiFormRef = ref<ElFormInstance>();


const emit = defineEmits(['close', 'success']);
const props = defineProps({
  cameraAiId: String | Number
});

const initFormData: cameraAiForm = {
  cameraAiId: undefined,
  remark: undefined,
  cameraAiName: undefined
}
const data = reactive<PageData<cameraAiForm, cameraAiQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    cameraAiName: undefined,
    params: {
    }
  },
  rules: {
    remark: [
      { required: true, message: "备注不能为空", trigger: "blur" }
    ],
    cameraAiName: [
      { required: true, message: "ai算法名称不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  cameraAiFormRef.value?.resetFields();
}

/** 获取ai算法详情 */
const getcameraAiInfo = async () => {
  proxy?.$modal.loading('加载中...')
const res = await getcameraAi(props.cameraAiId).finally(() => proxy?.$modal.closeLoading());
  Object.assign(form.value, res.data);
}
/** 提交按钮 */
const submitForm = () => {
  cameraAiFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      await updatecameraAi(form.value).finally(() =>  buttonLoading.value = false);
      
      proxy?.$modal.msgSuccess("操作成功");
      emit('close');
      reset();
      emit('success');
    }
  });
}

onMounted(() => {
  getcameraAiInfo();
});
</script>
<style lang="scss" scoped>

</style>
